<template>
  <div id="biye4">
    <div class="step-one">
      <div class="s-title">课程体系与毕业要求的支撑关系矩阵</div>
      <div class="s-fazhi">
        达成阈值（合格标准）：
        <!-- 数值 -->
        <el-input
          v-if="isEditScore"
          v-model="score"
        />
        <span v-else>{{ score }}</span>
        <!-- 点击 -->
        <span v-if="isEditScore">
          <el-icon @click="editScore">
            <Check />
          </el-icon>
          <el-icon @click="handleEditScoreCancel">
            <Close />
          </el-icon>
        </span>
        <el-icon
          v-else
          @click="handleEditScore"
        >
          <Edit />
        </el-icon>
      </div>
      <div
        v-if="isEditLook"
        class="s-form"
        id="info-view"
      >
        <div class="s-form-title">
          {{ lookName }}
          <div
            class="s-btn"
            @click="addLookRow"
          >
            <el-icon>
              <CirclePlus />
            </el-icon>
            课程
          </div>
        </div>
        <div class="s-form-content">
          <div
            v-for="(item, index) in lookList"
            :key="item.id"
            class="s-row"
          >
            <div>
              <div class="s-label">支撑课程</div>
              <div class="s-input">
                <el-select v-model="item.courseId">
                  <el-option
                    v-for="item in courseList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </div>
            </div>
            <div>
              <div class="s-label">权重 (权里值之和等于1,精确至两位小数）</div>
              <div class="s-input">
                <el-input v-model="item.score" />
              </div>
            </div>
            <div>
              <div class="s-label">支撑度</div>
              <div class="s-input">
                <el-select v-model="item.type">
                  <el-option
                    v-for="item in typeOptions"
                    :key="item.type"
                    :label="item.name"
                    :value="item.type"
                  />
                </el-select>
              </div>
            </div>
            <div
              class="s-icon"
              @click="deleteLookRow(index)"
            >
              <el-icon>
                <Delete />
              </el-icon>
            </div>
          </div>
          <div class="s-row s-form-result">
            <div class="s-input">合计:{{ courseSum }}</div>
            <div class="s-input">权重和：{{ weightSum }}</div>
            <div class="s-input s-resbtn">
              <div @click="editLook">
                <el-icon>
                  <Check />
                </el-icon>
                保存
              </div>
              <div @click="isEditLook = false">
                <el-icon>
                  <Close />
                </el-icon>
                取消
              </div>
            </div>
            <div class="s-icon">
              <el-icon>
                <Delete />
              </el-icon>
            </div>
          </div>

        </div>
      </div>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :span-method="objectSpanMethod"
        ref="tableRef"
        class="my-table"
      >
        <el-table-column
          label="毕业要求"
          width="300"
        >
          <template #default="{row}">
            <div>
              <el-tooltip
                effect="dark"
                :content="row.content"
                placement="top"
              >
                <strong>{{ row.name }}</strong>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>

        <!-- 动态列：每个培养目标一列 -->
        <el-table-column
          v-for="(course, index) in courseNum"
          :key="index"
          :label="'课程'+(index+1)"
          align="center"
        >
          <template #default="scope">
            {{ scope.row.courseName?.[index] || '—' }}
          </template>
        </el-table-column>
        <el-table-column
          label="总计"
          width="100"
        >
          <template #default="{row}">
            <div><strong>{{ row.num }}</strong></div>
          </template>
        </el-table-column>
        <el-table-column
          label="编辑"
          width="100"
        >
          <template #default="{row}">
            <div
              class="blue"
              @click="getLook(row.id,row.name)"
            >
              <el-icon>
                <Edit />
              </el-icon>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
  
  <script>
export default {
  components: {},
  data() {
    return {
      requirementsId: "",
      weightSum: 0,
      typeOptions: [
        {
          type: 1,
          name: "强",
        },
        {
          type: 2,
          name: "弱",
        },
      ],
      requirementsId: "",
      isEditLook: false,
      lookList: [],
      courseList: [],
      isEditScore: false,
      scoreId: "",
      score: "",
      scoreCopy: "",
      radio1: 1,
      editTwo: false,
      twoMessage:
        "培养系统地掌握数学、自然科学基础知识和计算机科学与技术专业基础知识，具备较坚实的理论基础，获得计算加专业良好的科学思维和系统的工程训练，具备良好的分析问题解决问题的能力及良好外语运用能力，具有强烈的自主学习意识和创新意识，科学型和工程型相结合的计算机专业高级人才。",
      tableData: [],
    };
  },
  computed: {
    // 课程数量
    courseSum() {
      return this.lookList.length;
    },
    // 权重和（保留2位小数）
    weightSum() {
      let sum = 0;
      this.lookList.forEach((item) => {
        const score = Number(item.score);
        if (!isNaN(score)) {
          sum += score;
        }
      });
      return sum.toFixed(2);
    },
  },
  created() {
    this.getScore();
    this.getList();
    this.getCourseList();
  },
  methods: {
    //跳转到指定元素
    scrollIntoView() {
      const element = document.getElementById("info-view");
      if (element) {
        element.scrollIntoView({
          behavior: "smooth",
          block: "start",
        });
      }
    },
    // 注释下面方法
    // 点击取消编辑合格保准按钮
    handleEditScoreCancel() {
      this.score = this.scoreCopy;
      this.isEditScore = false;
    },
    // 点击编辑合格标准按钮
    handleEditScore() {
      this.scoreCopy = this.score;
      this.isEditScore = true;
    },
    // 点击删除课程按钮
    deleteLookRow(index) {
      this.lookList.splice(index, 1);
    },
    // 点击添加课程按钮
    addLookRow() {
      this.lookList.push({
        requirementsId: this.requirementsId,
        courseId: "",
        score: 0,
        type: 1,
      });
    },
    //获取分数
    getScore() {
      const url = "/tes/unauthorized/config";
      const params = {
        keys: "graduation_qualified_standard_score",
      };
      this.$axios
        .get(url, { params })
        .then((response) => {
          const res = response.data;
          console.log(res.graduation_qualified_standard_score[0].keyValue, 22);
          this.score = res.graduation_qualified_standard_score[0].keyValue;
          this.scoreId = res.graduation_qualified_standard_score[0].id;
        })
        .finally(() => {});
    },
    //修改分数
    editScore() {
      const url = "/tes/unauthorized/config";
      const data = [
        {
          id: this.scoreId,
          keyWord: "graduation_qualified_standard_score",
          keyValue: this.score,
        },
      ];
      this.$axios
        .put(url, data)
        .then((response) => {
          const res = response;
          this.$message.success(res.message || "修改成功");
          this.isEditScore = false;
        })
        .finally(() => {});
    },
    //获取列表
    getList() {
      const url = "/tes/graduation/requirements-course";
      this.$axios
        .get(url)
        .then((response) => {
          const res = response.data;
          this.courseNum = res.courseNum;
          this.tableData = res.rows;
        })
        .finally(() => {});
    },
    //保存前的查看 通过id获取row
    getLook(id, name) {
      this.lookName = name;
      const url = "/tes/graduation/requirements-course-get";
      this.requirementsId = id;
      const params = {
        id: id,
      };
      this.$axios
        .get(url, { params })
        .then((response) => {
          const res = response.data;
          this.lookList = res;
          this.requirementsId = id;
          this.isEditLook = true;
          this.$nextTick(() => {
            this.scrollIntoView();
          });
        })
        .finally(() => {});
    },
    //获取课程列表
    getCourseList() {
      const url = "/tes/course/share-course";
      this.$axios
        .get(url)
        .then((response) => {
          const res = response.data;
          this.courseList = res;
        })
        .finally(() => {});
    },
    //保存修改
    editLook() {
      const url = "/tes/graduation/requirements-course";
      console.log(this.lookList, "123");
      const data = { requirementsId: this.requirementsId, data: this.lookList };
      this.$axios
        .post(url, data)
        .then((response) => {
          const res = response;
          this.$message.success(res.message || "保存成功");
          this.isEditLook = false;
          this.getList();
        })
        .finally(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
#biye4 {
  background: #ffffff;
  box-shadow: 0px 0px 7px 0px rgba(70, 123, 235, 0.13);
  border-radius: 10px;
  border: 1px solid #ffffff;
  padding: 26px;
  box-sizing: border-box;
  .s-right {
    display: flex;
    justify-content: flex-end;
    .el-icon {
      font-size: 20px;
    }
  }
  .s-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 4px;
    background: #457aff;
    border-radius: 4px;
    font-size: 14px;
    color: #ffffff;
    padding: 9px 15px;
    cursor: pointer;
    &:hover {
      background: #3a68c7;
    }
  }

  .my-table {
    :deep(table) {
      th,
      td {
        padding: 2px;
        text-align: center;
        border: 0;
      }
      th {
        font-weight: bold;
        font-size: 16px;
        color: #ffffff;
        .cell {
          height: 71px;
          line-height: 71px;
          background: #a6b4ec;
          margin-bottom: 4px;
        }
      }
      td {
        .cell {
          height: 44px;
          line-height: 44px;
          background: #f6f9ff;
          .green {
            font-size: 20px;
            color: #48c978;
          }
          .red {
            font-size: 20px;
            color: #ff6666;
          }
          .blue {
            font-size: 18px;
            color: #457aff;
            cursor: pointer;
            &:hover {
              background: #e6e9f2;
            }
          }
        }
      }
    }
  }
  .step-one {
    margin-bottom: 37px;
    .s-title {
      font-size: 16px;
      color: #1e2c48;
      margin-bottom: 20px;
    }
    .s-fazhi {
      display: flex;
      align-items: center;
      font-size: 14px;
      color: #71778f;
      margin-bottom: 10px;
      span {
        font-weight: bold;
        font-size: 14px;
        color: #1e2c48;
      }
      .el-icon {
        margin-left: 10px;
        color: #457aff;
        font-size: 20px;
        cursor: pointer;
        &:hover {
          color: #3a68c7;
        }
      }
      .el-input {
        width: 100px;
      }
      padding-bottom: 20px;
      border-bottom: 1px solid #eaecf0;
    }
    .s-form {
      margin-bottom: 40px;
      .s-form-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;
      }
      .s-form-content {
        .s-row {
          display: flex;
          gap: 10px;
          margin-bottom: 10px;
          &.s-form-result {
            border-top: 1px solid #eaecf0;
            padding-top: 15px;
            margin-top: 20px;
            .s-input {
              color: #1e2c48;
            }
            .s-icon {
              visibility: hidden;
            }
            .s-resbtn {
              display: flex;
              justify-content: flex-end;
              > div {
                display: flex;
                align-items: center;
                gap: 4px;
                font-size: 14px;
                color: #457aff;
                cursor: pointer;
                .el-icon {
                  font-size: 18px;
                }
                &:hover {
                  color: #3a68c7;
                }
              }
            }
          }
          > div {
            width: 100%;
            display: flex;
            align-items: center;
            gap: 10px;
            &.s-icon {
              width: 50px;
              color: #457aff;
              cursor: pointer;
              &:hover {
                color: #3a68c7;
              }
            }
            .s-label {
              font-size: 14px;
              color: #71778f;
            }
            .s-input {
              flex: 1;
              .el-select {
                width: 100%;
              }
            }
          }
        }
      }
    }
    .s-content {
      font-size: 14px;
      color: #71778f;
      line-height: 24px;
      margin-bottom: 35px;
    }
  }
}
</style>